package com.it.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.it.entity.*;
import com.it.service.ClassifyService;
import com.it.service.InventoryService;
import com.it.service.ProductService;
import com.it.service.WbeParameterService;
import com.it.util.ItdragonUtils;
import com.it.util.Result;
import com.it.util.ResultResponse;
import com.it.util.TableResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈商品实现接口〉<br>
 * @since 1.0.0
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private WbeParameterService wbeParameterService;

    /**
     * 商品管理界面跳转
     *
     * @param mv
     * @return
     */
    @RequestMapping("/index.do")
    public ModelAndView enIndex(ModelAndView mv) {
        //获取商品父分类
        List<Classify> classifyList = classifyService.selectList();
        mv.addObject("classifyList", classifyList);
        mv.setViewName("product/index");
        return mv;
    }

    /**
     * 商品列表加载接口
     *
     * @param product
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @GetMapping("productTable.do")
    public TableResultResponse productTable(Product product, int page, int limit) {
        //预警阈值查询
        WbeParameter wbeParameter = wbeParameterService.getWbeParameter();
        Integer threshold = wbeParameter.getThreshold();
        if (threshold == null) {
            threshold = 0;
        }
        List<Map<String, Object>> infoList = new ArrayList<>();
        Page<Product> pageInfo = productService.selectPage(product, page, limit);
        for (Product record : pageInfo.getRecords()) {
            Map<String, Object> resultMap = new HashMap<>(16);
            resultMap.put("id", record.getId());
            resultMap.put("name", record.getName());
            resultMap.put("uuId", record.getUuId());
            resultMap.put("price", record.getPrice());
            if (ItdragonUtils.stringIsNotBlack(record.getImg())) {
                resultMap.put("img", " <a href=\"" + record.getImg() + "\">查看图片</a>");
                resultMap.put("imgUrl", record.getImg());
            } else {
                resultMap.put("img", "暂无商品图片");
            }

            //算出会员价
            if (record.getDiscount() == null || record.getDiscount() == 0) {
                resultMap.put("vipPrice", "无");
            } else {
                DecimalFormat df = new DecimalFormat("#.00");
                resultMap.put("vipPrice", df.format(record.getPrice() * record.getDiscount()));
            }
            resultMap.put("createTime", record.getCreateTime() == null ? "" : record.getCreateTime().substring(0, 19));
            resultMap.put("discount", record.getDiscount());
            resultMap.put("parameter", record.getParameter());
            resultMap.put("particulars", record.getParticulars());
            resultMap.put("classifyId", record.getClassifyId());
            resultMap.put("chdClassId", record.getChdClassId());
            //库存查询
            List<Inventory> inventories = inventoryService.getOneByProductId(record.getId());
            if (!inventories.isEmpty()) {
                if (inventories.get(0).getStock() >= threshold) {
                    resultMap.put("inventories", inventories.get(0).getStock());
                } else {
                    resultMap.put("inventories", "  <span style=\"color: red\">库存警告</span>");
                }
            } else {
                resultMap.put("inventories", "  <span style=\"color: red\">库存为0</span>");
            }
            if (ItdragonUtils.stringIsNotBlack(record.getClassifyId()) && ItdragonUtils.stringIsNotBlack(record.getChdClassId())) {
                resultMap.put("classifyName", classifyService.getClassify(record.getClassifyId()).getName() +
                        "==>" + classifyService.getChdClassify(record.getChdClassId()).getName());
            }
            infoList.add(resultMap);
        }
        return Result.tableResule(pageInfo.getTotal(), infoList);
    }

    /**
     * 删除商品
     *
     * @param id
     * @return
     */
    @ResponseBody
    @DeleteMapping("/product.do")
    public ResultResponse delClassify(String id) {
        boolean result = productService.delById(id);
        if (!result) {
            return Result.resuleError("删除失败");
        }
        return Result.resuleSuccess();
    }

    /**
     * 新增商品界面跳转接口
     *
     * @param mv
     * @return
     */
    @RequestMapping("/addProduct.do")
    public ModelAndView addProduct(ModelAndView mv) {
        //获取商品父分类
        List<Classify> classifyList = classifyService.selectList();
        mv.addObject("classifyList", classifyList);
        mv.setViewName("product/addProduct");
        return mv;
    }

    /**
     * 新增商品
     *
     * @param product
     * @return
     */
    @ResponseBody
    @PostMapping("/product.do")
    public ResultResponse insertProduct(Product product) {
        boolean result = productService.insert(product);
        if (!result) {
            return Result.resuleError("新增失败");
        }
        return Result.resuleSuccess();
    }

    /**
     * 编辑商品界面跳转接口
     *
     * @param mv
     * @return
     */
    @RequestMapping("/editProduct.do")
    public ModelAndView editProduct(ModelAndView mv, String id) {
        //获取商品父分类
        List<Classify> classifyList = classifyService.selectList();
        mv.addObject("classifyList", classifyList);
        //获取商品子分类
        List<ChdClassify> chdClassifyList = classifyService.selectChdClassifyList();
        mv.addObject("chdClassifyList", chdClassifyList);
        Product product = productService.getOne(id);
        mv.addObject("product", product);
        mv.setViewName("product/editProduct");
        return mv;
    }

    /**
     * 编辑商品
     *
     * @param product
     * @return
     */
    @ResponseBody
    @PutMapping("/product.do")
    public ResultResponse updataProduct(Product product) {
        boolean result = productService.editById(product);
        if (!result) {
            return Result.resuleError("编辑失败");
        }
        return Result.resuleSuccess();
    }

}